#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_STUDENTS 100 // 班级最大学生数

// 学生结构体
typedef struct {
    char name[50]; // 姓名
    int id;        // 学号
    char gender;   // 性别（M/F）
    int age;       // 年龄
    float score;   // 成绩
} Student;

// 班级结构体
typedef struct {
    Student students[MAX_STUDENTS]; // 学生数组
    int count;                       // 当前学生数量
} Class;


//退出函数
void exit_(){
    char input;
    printf("\n\n\n按回车键返回上级目录" );
    while (getchar() != '\n');
    while (1) {
        input = getchar();
        if (input == '\n') {
            printf("正在退出中...\n");
            sleep(1);
            break;
        }
    }
}


// 函数：添加学生
void add_student(Class *class) {
    if (class->count >= MAX_STUDENTS) {
        system("clear");
        printf("班级已满，无法添加新学生！\n");
        exit_();
    }
    system("clear");
    Student new_student;
    printf("请输入学生姓名: ");
    scanf("%s", new_student.name);
    printf("请输入学生学号: ");
    scanf("%d", &new_student.id);
    printf("请输入学生性别 (M/F): ");
    scanf(" %c", &new_student.gender);
    printf("请输入学生年龄: ");
    scanf("%d", &new_student.age);
    printf("请输入学生成绩: ");
    scanf("%f", &new_student.score);

    class->students[class->count] = new_student;
    class->count++;
    system("clear");
    printf("学生添加成功！\n");
    exit_();
}

// 函数：删除学生
void delete_student(Class *class) {
    int id;
    printf("请输入要删除的学生学号: ");
    scanf("%d", &id);

    for (int i = 0; i < class->count; i++) {
        if (class->students[i].id == id) {
            // 将后面的学生向前移动
            for (int j = i; j < class->count - 1; j++) {
                class->students[j] = class->students[j + 1];
            }
            class->count--;
            system("clear");
            printf("学生删除成功！\n");
            exit_();
            return;
        }
    }
    system("clear");
    printf("未找到该学生！\n");
    exit_();
}

// 函数：修改学生信息
void modify_student(Class *class) {
    int id;
    printf("请输入要修改的学生学号: ");
    scanf("%d", &id);

    for (int i = 0; i < class->count; i++) {
        if (class->students[i].id == id) {
            printf("请输入新的学生姓名: ");
            scanf("%s", class->students[i].name);
            printf("请输入新的学生性别 (M/F): ");
            scanf(" %c", &class->students[i].gender);
            printf("请输入新的学生年龄: ");
            scanf("%d", &class->students[i].age);
            printf("请输入新的学生成绩: ");
            scanf("%f", &class->students[i].score);
            system("clear");
            printf("学生信息修改成功！\n");
            exit_();
            return;
        }
    }
    system("clear");
    printf("未找到该学生！\n");
    exit_();
}

// 函数：查询学生信息
void query_student(Class *class) {
    int id;
    printf("请输入要查询的学生学号: ");
    scanf("%d", &id);

    for (int i = 0; i < class->count; i++) {
        if (class->students[i].id == id) {
            system("clear");
            printf("姓名: %s\n", class->students[i].name);
            printf("学号: %d\n", class->students[i].id);
            printf("性别: %c\n", class->students[i].gender);
            printf("年龄: %d\n", class->students[i].age);
            printf("成绩: %.2f\n", class->students[i].score);
            exit_();
            return;
        }
    }
    system("clear");
    printf("未找到该学生！\n");
    exit_();
}


//改进后的查找：
//二分查找：
int binary_search(Class *class, float target_score) {
    int low = 0;
    int high = class->count - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        if (class->students[mid].score == target_score) {
            return mid; // 找到目标成绩，返回索引
        } else if (class->students[mid].score < target_score) {
            high = mid - 1; // 在左半部分查找
        } else {
            low = mid + 1; // 在右半部分查找
        }
    }

    return -1; // 未找到目标成绩
}


// 函数：按成绩查找学生信息
void search_by_score(Class *class) {
    float target_score;
    printf("请输入要查找的学生成绩: ");
    scanf("%f", &target_score);

    int index = binary_search(class, target_score);
    if (index != -1) {
        system("clear");
        printf("找到学生: 姓名: %s, 学号: %d, 性别: %c, 年龄: %d, 成绩: %.2f\n",
               class->students[index].name, class->students[index].id,
               class->students[index].gender, class->students[index].age,
               class->students[index].score);
               exit_();
    } else {
        system("clear");
        printf("未找到成绩为 %.2f 的学生！\n", target_score);
        exit_();
    }
}




// 函数：按成绩排序学生
void sort_students(Class *class) {
    for (int i = 0; i < class->count - 1; i++) {
        for (int j = 0; j < class->count - i - 1; j++) {
            if (class->students[j].score < class->students[j + 1].score) {
                // 交换学生信息
                Student temp = class->students[j];
                class->students[j] = class->students[j + 1];
                class->students[j + 1] = temp;
            }
        }
    }
    system("clear");
    printf("学生按成绩排序成功！\n");
    exit_();
}

// 函数：显示所有学生信息
void display_students(Class *class) {
    if (class->count == 0) {
        system("clear");
        printf("班级中没有学生！\n");
        exit_();
    }
    system("clear");

    printf("班级学生信息如下：\n");
    for (int i = 0; i < class->count; i++) {
        printf("姓名: %s\t学号: %d\t性别: %c\t年龄: %d\t成绩: %.2f\n",
               class->students[i].name, class->students[i].id,
               class->students[i].gender, class->students[i].age,
               class->students[i].score);
    }
    exit_();
}

// 主函数
int main() {
    Class class = { .count = 0 }; // 初始化班级
    int choice;

    while (1) {
        system("clear");
        printf("\n=== 学生管理系统 ===\n");
        printf("1. 添加学生\n");
        printf("2. 删除学生\n");
        printf("3. 修改学生信息\n");
        printf("4. 查询学生信息\n");
        printf("5. 按成绩排序学生\n");
        printf("6. 按成绩查找学生\n");
        printf("7. 显示所有学生信息\n");
        printf("8. 退出\n");
        printf("请输入选项: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_student(&class);
                break;
            case 2:
                delete_student(&class);
                break;
            case 3:
                modify_student(&class);
                break;
            case 4:
                query_student(&class);
                break;
            case 5:
                sort_students(&class);
                break;
            case 6:
                search_by_score(&class);
                break;
            case 7:
                display_students(&class);
                break;
            case 8:
                printf("感谢使用，再见！\n");
                exit(0);
            default:
                printf("无效选项，请重新选择！\n");
        }
    }

    return 0;
}